require("../less/training.less");
document.ready(function() {
    window.$utils.exerHeader('training');
    window.$utils.footerFn('exercise');

    //服务器地址
    axios.defaults.baseURL = "http://139.9.177.51:8099";


    coursRender();

    function coursRender() {
        axios.get("/sports/allcourse")
            .then(function(res) {
                console.log(res.data);

                //最新课程渲染
                //筛选出最新课程
                let latestData = res.data.data.filter(function(v) {
                        return v.latest == 1;
                    })
                    // 添加自定义属性，把课程id添加上去
                document.querySelector('.latestCourse').dataset['cid'] = latestData[0].courseId;
                document.querySelector('#latestImg').src = "http://139.9.177.51:8099" + latestData[0].imgurl;
                document.querySelector('#latestName').textContent = latestData[0].name;
                document.querySelector('#latestDesc').textContent = latestData[0].desc;

                //筛选出旧课程
                let formerData = res.data.data.filter(function(v) {
                        return v.latest == 0;
                    })
                    //创建旧课程节点
                formerData.map(function(v) {
                    let li = document.createElement('li');
                    li.dataset['cid'] = v.courseId;
                    li.style.backgroundImage = `url(http://139.9.177.51:8099${v.imgurl})`
                    li.innerHTML = `
                    <h6>${v.name}</h6>
                    <p>${v.desc}</p>
                    `
                    document.querySelector('.course').append(li);
                })
            })
            .catch(function(err) {
                console.log(err);
            })
    }

    //点击课程(最新课程和普通课堂)，本地存储
    document.querySelector('.latestCourse').addEventListener('click', function() {
        sessionStorage.setItem('courseId', this.dataset['cid']);
        location.href = './description.html';
    })
    document.querySelector('.course').addEventListener('click', function(e) {
        if (e.target.nodeName == 'LI') {
            sessionStorage.setItem('courseId', e.target.dataset['cid']);
            location.href = './description.html'
        }
        //当点到文字时
        if (e.target.closest('li').nodeName == 'LI') {
            console.log(e.target);
            sessionStorage.setItem('courseId', e.target.closest('li').dataset['cid']);
            location.href = './description.html';
        }



    })


})